home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
6984
/
6984.xpi
/
modules
/
crypto.js
next >
Wrap
Text File
|
2009-11-24
|
5KB
|
132 lines
var EXPORTED_SYMBOLS = ["Crypto"];
/**
* Lazarus Cryptography
* We will use hybrid encryption when saving data to the database.
* this should allow us to encrypt forms with the public key and only require the user to
* enter their password when they want to retrieve a form.
*
* Both the public and private (RSA) keys should be kept in the database.
* The public key is kept unencrypted, but the private key will be symetrically encrypted (AES)
* using the users password.
*/
var Crypto = {
AESStaticIV: '0gL6pSJ13AKDvo7xNnsZaQ==',
publicKey: null,
privateKey: null,
//flag to indicate if we are currently generating RSA keys
generatingKeys: false,
crypto: Components.classes["@labs.mozilla.com/Weave/Crypto;1"].createInstance(Components.interfaces.IWeaveCrypto),
/**
* encrypt a string
*/
encrypt: function(str){
//generate a random string with which we're going to encode this data.
var randKey = this.crypto.generateRandomKey();
//encrypt the data with the random key
var encData = this.crypto.encrypt(str, randKey, this.publicKey.iv);
//now encrypt the key with our public/private key encryption
var encKey = this.crypto.wrapSymmetricKey(randKey, this.publicKey.key);
//and return the encrypted key with the data
return encKey +":"+ encData;
},
decrypt: function(encStr){
//split the string into key/data
var encKey = encStr.substr(0, encStr.indexOf(":"));
var encData = encStr.substr(encStr.indexOf(":")+1);
//decrypt the random key
var decKey = this.crypto.unwrapSymmetricKey(encKey, this.privateKey.key,
this.privateKey.password,
this.privateKey.passphraseSalt,
this.privateKey.privkeyWrapIV);
//then decrypt the data with the unencrypted random key
return this.crypto.decrypt(encData, decKey, this.privateKey.iv);
},
/**
* AES encryption/decryption methods appear to take passwords of the exact length (256 bits)
*/
fixPassword: function(password){
//password must be 256 bits long
var PASSWORD_LEN = 32; //characters
password = password.substr(0, PASSWORD_LEN);
var origLen = password.length;
for (var i=origLen; i<PASSWORD_LEN; i++){
password += " ";
}
return btoa(password);
},
AESEncrypt: function(str, password){
return this.crypto.encrypt(str, this.fixPassword(password), this.AESStaticIV);
},
AESDecrypt: function(encStr, password){
try {
return this.crypto.decrypt(encStr, this.fixPassword(password), this.AESStaticIV);
}
catch(e){
return false;
}
},
generateRSAKeyPair: function(){
var privOut = {};
var pubOut = {};
var publicKeyIV = this.crypto.generateRandomIV();
var password = this.crypto.generateRandomBytes(32)
var salt = this.crypto.generateRandomBytes(32);
var iv = this.crypto.generateRandomIV();
try {
this.generatingKeys = true;
this.crypto.generateKeypair(password, salt, iv, pubOut, privOut);
this.generatingKeys = false;
return {
"public": {
key: pubOut.value,
iv: publicKeyIV,
},
"private": {
key : privOut.value,
password : password,
passphraseSalt : salt,
privkeyWrapIV : iv,
iv : publicKeyIV
}
}
}
catch(e){
this.generatingKeys = false;
var consoleService = Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);
var scriptError = Components.classes["@mozilla.org/scripterror;1"].createInstance(Components.interfaces.nsIScriptError);
scriptError.init(e.message, e.fileName, null, e.lineNumber, null, scriptError.errorFlag, "component javascript");
consoleService.logMessage(scriptError);
return false;
}
}
};